$(document).ready(function(){
	getUserRight();
	$("#functionNav").attr("href",navAddr);
	//获取并填充来源类型
	var lylxJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInJb.do",{'tableName':'jb_lyfs'});
	loadSelect("#form1 select[name=lylx]",lylxJson.rows,'id','name');
	//获取并填充时代
	getSdOption();
	//var doms_sd = ["DJH_SD1","DJH_SD2","DJH_SD3","DJH_SD4"];
	//selectFour(url_nd,doms_sd);
	//获取查询条件
	var params =$("#form1").serializeObject();
	//初始化待分配库房Table
  	createTable('#dbTable',ProjectName+'/colmanage.getDbCpList.do','id',tblColTitle,params);
  	//点击查询按钮
	 $("#search_btn").click(function(){
		 refreshTable('#dbTable','#form1');
	 });
  	
  	//获取并填充保管部门
	var bgbmJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInjb.do",{'tableName':'jb_bgbm'});
	loadSelect("#form_fpkf select[name=bgbm]",bgbmJson.rows,'id','name');
	
	//根据保管部门id获取并填充库房
	$('select[name=bgbm]').change(function(){
		var bgbmid = $(this).val();
		if(bgbmid !=null && bgbmid !=''){
			var kfJson = ajaxResult("get",false,ProjectName+"/sysmanage.getkfIdNameBybgbmId.do",{'bgbmId':bgbmid});
			loadSelect("select[name=szkf]",kfJson.rows,'kfid','kfmc');
		}
	});
  	
    //点击批量分配库房按钮，打开分配库房窗口
	 $("#fpkf_btn").click(function(){
		var ids = new Array();
		$("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
			 if ("checked" == $(this).attr("checked")) { 
				 ids.push($(this).attr('value'));							 
			 } 
		}); 	
		if(ids.length == 0){
			alertFunc("请选择记录！");
			return false;
		}		
		openModal(ids);
	 });
	 
	 //分配库房
	 $("#btn_add_fpkf").click(function(){
		if(checkform("#form_fpkf")){
		   var params =$("#form_fpkf").serializeObject();
		   var result="";
		   if(params.id == null || params.id == ""){
			   bootbox.setLocale("zh_CN");
			   result = ajaxResult("post",false,ProjectName+'/colmanage.dbFenpeiKf.do',params);
			   if(result["status"] == 'ok'){
				   alertFunc("分配库房成功！");
				   $('#fpkfModal').modal('hide');
				   refreshTable('#dbTable','#form1');
			   }else{
				   alertFunc(result["error"]);
			   }
	   }
		}
	 });
	 
	//退回登编
	 $("#returndb_btn").click(function(){			
			var ids = new Array();
			$("#dbTable input[name='btSelectItem']:checkbox").each(function(){ 
				 if ("checked" == $(this).attr("checked")) { 
					 ids.push($(this).attr('value'));							 
				 } 
			}); 	
			if(ids.length == 0){
				alertFunc("请选择记录！");
				return false;
			}
			
			bootbox.setLocale("zh_CN");
			bootbox.confirm("您确定退回登编吗？",function(r){
				if(r){
					result = ajaxResult("post",false,ProjectName+'/colmanage.resetDb.do',{'ids':ids+""});
					   if(result["status"] == 'ok'){
						   alertFunc("退回登编成功！");
						   refreshTable('#dbTable','#form1');
					   }else{
						   alertFunc(result["error"]);
					   }
				}
			});			
	 });	 	
})

//打开分配库房窗口
function openModal(ids){
	if(ids!=null && ids!=""){
		$("#form_fpkf input[name=ids]").val(ids);
		$('#fpkfModal').modal('show');
	}
}

var tblColTitle = [{
	 field: 'id',
	 title: '锁定',
	 class:'bs-checkbox',
	 formatter: function(value, row, index) {
		 var result ='';
		 if(row.sdbz==1){
			 if(row.sdrid == userInfo.userid){
				 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" zdjh="'+row.zdjh+'" value="'+row.id+'" checked="checked" onclick="dbrkUnLock('+row.id+')">';
			 }else{
				 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" zdjh="'+row.zdjh+'" value="'+row.id+'" disabled=true>';
			 }
		 }else{
			 result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" zdjh="'+row.zdjh+'" value="'+row.id+'" onclick="dbrkLock('+row.id+')">';
		 }
   	  	return result;
   	  }
    },{
	        field: 'zdjh',
	        title: '总登记号'
	    },{
	        field: 'cpmc',
	        title: '藏品名称',
	        formatter: function(value, row, index) {	     
	        	var result="";	  
    			if(row.sdbz==1 && row.sdrid == userInfo.userid)	   
        			result = '<a href="dbDetail.html?id='+row.id+'">'+value+'</a>';
    			else
    				result =value;
	        	return result;	        	
		      }
	    }, {
	        field: 'sd4',
	        title: '时代',
	        formatter: function(value, row, index) {
	        	var sd="";
	        	if(row.sd4!="" && row.sd4!=null){
	        		sd =row.sd4;
	        	}else if(row.sd3!="" && row.sd3!=null){
	        		sd =row.sd3;
	        	}else if(row.sd2!="" && row.sd2!=null){
	        		sd =row.sd2;
	        	}else if(row.sd1!="" && row.sd1!=null){
	        		sd =row.sd1;
	        	}
	        	return sd;
		      }
	    }, {
	        field: 'sjsl',
	        title: '实际数量',
	        formatter: function(value, row, index) {	        	
	        	return value+row.sjsldw;
		      }
	    }, {
	        field: 'lylxmc',
	        title: '来源方式'
	    }, {
	        field: 'sdrmc',
	        title: '锁定人',
	        formatter: function(value, row, index) {
	        	  if(row.sdbz==1)
	        		  return value;
	        	  else
	        		  return "";
		      	}
	    },{
	        field: '',
	        title: '操作',
	        formatter: function(value, row, index) {
	        	var result="";	  
    			if(row.sdbz==1 && row.sdrid == userInfo.userid)	   
        			result = '<a href="javascript:void(0);" onclick="openModal('+row.id+')">分配库房</a>';
	        	return result;
		      }
	    }];
